package com.limbo.showapp.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.limbo.showapp.R;
import com.limbo.showapp.bean.CartoonBean;
import com.limbo.showapp.constant.AppConstant;
import com.limbo.showapp.widget.OnMultiClickListener;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import static com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions.withCrossFade;

/**
 * Created by wangqi on 2018/9/6.
 */

public class CartoonAdapter extends CommonAdapter<CartoonBean.Cartoon> {


    public CartoonAdapter(Context context) {
        super(context, R.layout.item_main_hot, new ArrayList<CartoonBean.Cartoon>());
    }

    public void setData(List<CartoonBean.Cartoon> list){
        List<CartoonBean.Cartoon> datas = getDatas();
        datas.clear();
        datas.addAll(list);
        notifyDataSetChanged();
    }

    public void addData(List<CartoonBean.Cartoon> list){
        List<CartoonBean.Cartoon> datas = getDatas();
        datas.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    protected void convert(final ViewHolder holder, CartoonBean.Cartoon bean, final int position) {
        ImageView ivImg = holder.getView(R.id.iv_img);
        TextView tvTime = holder.getView(R.id.tv_time);
        TextView tvTitle = holder.getView(R.id.tv_title);
        TextView tvCount = holder.getView(R.id.tv_count);
        String url = AppConstant.Url.BASE_URL + bean.getImgUrl();

        //压缩图片
        RequestOptions options = new RequestOptions()
//                .placeholder(R.drawable.img_loading)
//                .error(R.drawable.img_error)
                //设置图片加载的优先级
                .priority(Priority.HIGH)
                //缓存策略,跳过内存缓存【此处应该设置为false，否则列表刷新时会闪一下】
                .skipMemoryCache(false)
                //缓存策略,硬盘缓存-仅仅缓存最终的图像，即降低分辨率后的（或者是转换后的）
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        Glide.with(mContext).load(url)
                .transition(withCrossFade())
                .thumbnail(0.1f)
                .apply(options)
                .into(ivImg);

        tvTitle.setText(bean.getTitle());
        tvTime.setText(bean.getUpdateTime());
        tvCount.setText(bean.getUpCount());

        holder.itemView.setOnClickListener(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View v) {
                mOnItemClickListener.onItemClick(v,holder,position);
            }
        });
    }
}
